{$CLEO .cs}
{$USE CLEO+}
{$USE Audio}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "eCoj"

// By ArtemQa146

const
    car_1 = 0@
    music = 31@
    volume_ini = 30@
    price_min = 29@
    check = 28@
    car_id = 27@
    car_id_true = 26@
    volume_samp = 25@
end

0AF2: volume_ini = read_float_from_ini_file "cleo\Door with chime.ini" section "Settings" key "Volume"
0AF2: volume_samp = read_float_from_ini_file "cleo\Door with chime.ini" section "Settings" key "Volume SAMP"
0AF0: price_min = read_int_from_ini_file "cleo\Door with chime.ini" section "Settings" key "Price Minimum"

music = -1
car_id = -1
car_id_true = -1

while true
wait 0
    if
        player.Defined(0)
    then
        3@ = false
        0A96: 1@ = ped $PLAYER_ACTOR struct
        0D4E: 1@ = struct 1@ offset 0x58C size 4
        if
            1@ > 0
        then
            0AEB: car_1 = vehicle_struct 1@ handle
            3@ = true
        end

        0E43: get_char_task_pointer_by_id $player_actor 800 store_to 2@
        if
            2@ <> 0
        then
            0D4E: 1@ = read_struct 2@ offset 0xc size 4 // CTaskComplexCarDrive struc -> C m_pVehicle      dd ?
            0A96: 2@ = ped $PLAYER_ACTOR struct
            0E28: write_struct 2@ offset 0x58C size 4 value 1@
            0A96: 1@ = ped $PLAYER_ACTOR struct
            0D4E: 1@ = struct 1@ offset 0x58C size 4
            0AEB: car_1 = vehicle_struct 1@ handle
            if
                0EE5: locate_char_distance_to_car $PLAYER_ACTOR car car_1 radius 10.0
            then
                3@ = true
            end
        end
        
        if
            3@ == true
        then 
            0E12: get_vehicle car_1 subclass_to 2@
            if and
                2@ == 0
                8119:   not car car_1 wrecked
                0EE5: locate_char_distance_to_car $PLAYER_ACTOR car car_1 radius 25.0
            then
                check = false
                
                0441: 3@ = car car_1 model
                09E1: get_car_model 3@ price_to 4@
                if
                    002D:   4@ >= price_min  // (int)
                then
                    check = true
                end
                
                if
                    car_id == -1
                then
                    0441: car_id = car car_1 model
                    0AD3: 5@v = string_format "%d" car_id
                    0AF0: car_id_true = read_int_from_ini_file "cleo\Door with chime.ini" seaction "Car ID" key 5@v
                    if
                        car_id_true == 1
                    then
                        check = true
                    else
                        if
                            car_id_true == -1
                        then
                            continue
                        end
                    end
                else
                    if
                        803B:   not 3@ == car_id  // (int)
                    then
                        car_id = -1
                    else
                        if
                            car_id_true == 1
                        then
                            check = true
                        else
                            if
                                car_id_true == -1
                            then
                                continue
                            end
                        end
                    end
                end

                
                if
                    check == true
                then
                    095F: get_car car_1 door 2 angle_to 1@
                    095F: get_car car_1 door 3 angle_to 2@
                    if or
                        1@ > 0.2
                        2@ > 0.2
                        1@ < -0.2
                        2@ < -0.2
                    then
                        if
                            0E20: is_on_samp
                        then
                            if
                                music == -1
                            then
                                0AAC: music = load_audio_stream "sound/dwcsound.mp3"
                                0AAD: set_audio_stream music state 1
                                0ABC: set_audio_stream music volume volume_ini
                            else
                                0AB9: get_audio_stream music state_to 1@
                                if
                                    1@ == -1
                                then
                                    0AAD: set_audio_stream music state 1
                                else
                                    0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 1@ 2@ 3@
                                    0407: store_coords_to 4@ 5@ 6@ from_car car_1 with_offset 0.0 0.0 0.0
                                    050A: 7@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 5@ 6@
                                    7@ /= 100.0
                                    7@ *= 2.0
                                    0087: 8@ = volume_samp // (float)
                                    0063: 8@ -= 7@  // (float)
                                    if 8@ > 1.0
                                    then 8@ = 1.0
                                    end
                                    if 8@ < 0.0
                                    then 8@ = 0.0
                                    end
                                    0ABC: set_audio_stream music volume 8@
                                end
                            end
                        else
                            if
                                music == -1
                            then
                                0AC1: music = load_audio_stream_with_3d_support "sound/dwcsound.mp3" // IF and SET
                                0AC5: link_3d_audio_stream music to_car car_1
                                0AAD: set_audio_stream music state 1
                                0ABC: set_audio_stream music volume volume_ini
                            else
                                0AB9: get_audio_stream music state_to 1@
                                if
                                    1@ == -1
                                then
                                    0AAD: set_audio_stream music state 1
                                end
                            end
                        end
                    else
                        gosub @Remove_Audio
                    end
                else
                    gosub @Remove_Audio
                end
            else
                gosub @Remove_Audio
            end
        end
    end
end

:Remove_Audio
if
    not music == -1
then
    0AB9: get_audio_stream music state_to 1@
    if
        1@ == -1
    then
        0AAE: remove_audio_stream music
        music = -1
    end
end
return

:SAMP_2D_Sound

return